package org.example;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.List;

public class GmManger {
    /**
     * 用枚举来实现单例
     */
    private enum Singleton {
        INSTANCE;
        GmManger manger;

        Singleton() {
            this.manger = new GmManger();
        }

        GmManger getme() {
            return manger;
        }
    }

    /**
     * 获取实例对象
     *
     * @return
     */
    public static GmManger getme() {
        return Singleton.INSTANCE.getme();
    }

    /*
     *
     * 执行gm命令
     */
    public String gmOrder(String order) {
        if (order.length() <= 0) {
            return "gm命令无效";
        }
        String[] strings = order.split(" ");
        if (strings.length <= 0) {
            return "gm命令无效";
        }
        String gm = strings[0];//获取命令名称
        String[] params = new String[strings.length - 1];
        if (strings.length > 1) {
            System.arraycopy(strings, 1, params, 0, params.length);
        }
        try {
            Method declaredMethod = this.getClass().getDeclaredMethod(gm, String[].class);
            if (declaredMethod == null) {
                return "命令未匹配";
            }
            Object result = declaredMethod.invoke(this, (Object) params);
            if (result instanceof String) {
                return (String) result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "执行失败";
    }

    private String hello(String[] parm) {
        if (parm.length <= 0) {
            return "gm命令参数错误";
        }
        return "gm命令执行成功 " + parm[0];
    }

    private String additem(String[] parm) {
        if (parm.length != 2) {
            return "gm命令参数错误";
        }
        String format = String.format("gm命令执行成功,增加物品:%s 数量:%s", parm[0], parm[1]);
        return format;
    }

    /*private String addalliance(String[] parm) {
        Alliance alliance = new Alliance("name", "shortName", "publicNotice", 1, "bannerColour", 2, "totemColour", "token");
        AllianceServiceImpl instance = AllianceServiceImpl.getInstance();
        instance.addData(alliance);
        String format = String.format("gm命令执行成功,增加联盟:%s 数量:%s", parm[0], parm[1]);
        return format;
    }

    private String findallalliance(String[] parm) {
        AllianceServiceImpl instance = AllianceServiceImpl.getInstance();
        List<Alliance> allianceList = instance.selectAllDatas();
        for (Alliance alliance:allianceList ){
            System.out.println(alliance.toString());
        }
        return "查找成功";
    }*/

    //adcurrency 10
    private String adcurrency(String[] parm){
        String s = parm[0];
        BigDecimal bigDecimal = new BigDecimal(s);
        currency.getInstance().change(1,bigDecimal);
        return "";
    }
}
